import { baseURL } from './../../utils/api/http';
import { defineStore } from "pinia";
import { dictlist } from "../../utils/api/dict/dict";// 字典查询
import {
  getLossList,// 核销物资数据
  // deleteLoss,// 删除核销物资
  // deleteAllLoss,// 删除核销物资
  // getSuppliesList,// 查询仓库物资分页列表
  // saveDamage,// 新增核销单
  submitDamage,// 核销单提交
} from "../../utils/api/verifMange/verifMange";
// import { useMemberStore } from "@/stores/modules/member";
const quantityTitle = [
  { text: '物资编号', style: 'width:80px;', name: 'GoodsCode' },
  { text: '物资名称', style: 'width:120px;', name: 'GoodsName' },
  { text: '核销类型', style: 'width:70px;', name: 'CheckTypeName' },
  { text: '核销日期', style: 'width:172px;', name: 'CheckDate' },
  { text: '核销状态', style: 'width:70px;', name: 'CheckStatusName' },
  { text: '操作人', style: 'width:70px;', name: 'CheckOperatorName' },
  { text: '审批状态', style: 'width:80px;', name: 'CheckReplyStatusName' },
]// 核销管理表格标题

const lossAddTitle = [
  { text: '物资编号', style: 'width:130px;', name: 'GoodsCode' },
  { text: '物资名称', style: 'width:180px;', name: 'GoodsName' },
  { text: '批次/有效期', style: 'width:130px;', name: 'GoodsBatch' },// GoodsBatch 批次 ExpirationDate 有效期
  { text: '单位', style: 'width:70px;', name: 'Units' },
  { text: '报损数量', style: 'width:60px;', name: 'GoodsNumber' }
]// 核销新增

// const lossPopupTitle = [
//   { text: '物资名称', style: 'width:120px;', name: 'GoodsName' },
//   { text: '物资类型', style: 'width:70px;', name: 'GoodsTypeName' },
//   { text: '数量/单位', style: 'width:100px;', name: 'GoodsNumber' },
//   { text: '库位', style: 'width:180px;', name: 'BoxName' }
// ]// 核销新增弹窗

export const useVerifManageStore = defineStore(
  "verifManage",
  () => {
    const titleStatus = quantityTitle;// 核销管理表格标题
    const lossAddStatus = lossAddTitle;// 核销新增
    // const lossPopupStatus = lossPopupTitle;// 核销新增

    let verifTypeDict = ref([])// 核销类型字典
    let verifStatusDict = ref([])// 核销状态字典
    let total = ref(0)// 数据总条数
    let dataList = ref([])// 核销管理数据内容
    let lossAddList: any = ref([])// 核销新增数据内容

    const getDictData = async (data: object, mode: string) => {
      let res = await dictlist(data)
      let lossDict = res.data.map((item: AnyObject) => { return { text: item.DictName, value: item.DictCode } })
      switch (mode) {
        case 'verifTypeDict':
          verifTypeDict.value = lossDict
          console.log(verifTypeDict.value);
          break;

        case 'verifStatusDict':
          verifStatusDict.value = lossDict
          console.log(verifStatusDict.value);
          break;
        default:
          break;
      }
    }// 字典查询

    const getVerifListData = async (data: object) => {
      let res = await getLossList(data)
      total.value = res.data.totalCount
      dataList.value = res.data.list
    }// 获取核销管理数据

    // const deleteLossData = async (data: object) => {
    //   try {
    //     await deleteLoss(data)
    //     uni.showToast({
    //       title: '删除成功！',
    //       duration: 1000
    //     });
    //   } catch (error: any) {
    //     uni.showToast({
    //       title: error,
    //       icon: 'error',
    //       duration: 2000
    //     });
    //   }
    // }// 删除核销管理数据

    // const deleteLossAllData = async (data: object) => {
    //   try {
    //     await deleteAllLoss(data)
    //     uni.showToast({
    //       title: '删除成功！',
    //       duration: 1000
    //     });
    //   } catch (error: any) {
    //     uni.showToast({
    //       title: error,
    //       icon: 'error',
    //       duration: 2000
    //     });
    //   }
    // }// 批量删除核销管理数据

    // const getSupplies = getSuppliesList// 查询仓库物资分页列表

    // const lossSumbit = async (data: object, mode: string) => {
    //   uni.showLoading({
    //     title: mode === 'save' ? '正在保存' : '正在提交'
    //   });
    //   try {
    //     let res: any = await saveDamage(data)
    //     if (mode === 'sumbit') {
    //       console.log(res);
    //       console.log('sumbit');
    //       await submitDamage({ ...data, Uuid: res.Uuid })
    //     }
    //     uni.hideLoading()
    //     uni.showToast({
    //       title: mode === 'save' ? '保存成功' : '提交成功',
    //       icon: "success",
    //       duration: 1500
    //     });
    //     setTimeout(() => {
    //       uni.navigateBack()
    //     }, 1500);// 返回上一页
    //   } catch (error) {
    //     uni.hideLoading()
    //     uni.showToast({
    //       title: mode === 'save' ? '保存失败' : '提交失败',
    //       icon: "error",
    //       duration: 1500
    //     });
    //   }
    // }// 保存或提交核销单

    const modifySumbit = async (data: object, mode: string) => {
      uni.showLoading({
        title: '正在提交'
      });
      try {
        await submitDamage(data)
        uni.hideLoading()
        uni.showToast({
          title: '提交成功',
          icon: "success",
          duration: 1500
        });
        setTimeout(() => {
          uni.navigateBack()
        }, 1500);// 返回上一页
      } catch (error) {
        uni.hideLoading()
        uni.showToast({
          title: '提交失败',
          icon: "error",
          duration: 1500
        });
      }
    }// 修改或提交核销单

    return {
      titleStatus,
      lossAddStatus,
      verifTypeDict,
      verifStatusDict,
      total,
      dataList,
      lossAddList,
      getDictData,
      getVerifListData,
      modifySumbit
    };
  },
  // TODO: 持久化
  {
    // 配置持久化
    persist: {
      // 调整为兼容多端的API
      storage: {
        setItem(key, value) {
          uni.setStorageSync(key, value);
        },
        getItem(key) {
          return uni.getStorageSync(key);
        },
      },
      paths: ['verifTypeDict', 'verifStatusDict'],
    },
  }
);
